<?php
namespace app\admin\controller;

use app\common\cache\common\IAreaCache;
use app\common\cache\user\IUserCache;
use app\common\cache\user\IUserLevelCache;
use app\common\controller\AdminBaseController;
use app\common\enum\IsHistory;
use app\common\enum\UserStatus;
use app\common\ext\IDb;
use app\common\ext\IRequest;

class User extends AdminBaseController{

    /**
     * @功能：用户基础信息
     * @开发者：cxl
     * @return string
     */
    public function user_list(){
        //接收参数
        $user_id = IRequest::get('user_id');
        $agent_id = IRequest::get('agent_id');
        $level_id = IRequest::get('level_id');
        $nick = IRequest::get('nick');
        $phone = IRequest::get('phone');
        $identity = IRequest::get('identity');
        $real = IRequest::get('real');
        $is_social = IRequest::get('is_social');
        $is_accumulation_fund= IRequest::get('is_accumulation_fund');
        $is_marry = IRequest::get('is_marry');
        $is_white = IRequest::get('is_white');
        $is_loan = IRequest::get('is_loan');
        $is_overdue = IRequest::get('is_overdue');
        $is_student = IRequest::get('is_student');
        $is_real = IRequest::get('is_real');
        $is_car = IRequest::get('is_car');
        $is_house = IRequest::get('is_house');
        $is_credit_card = IRequest::get('is_credit_card');
        $is_policy = IRequest::get('is_policy');
        $is_company = IRequest::get('is_company');
        $source = IRequest::get('source');
        $area = IRequest::get('area');

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        //设置查询条件
        $where = null;
        if(!empty($user_id)) $where['user_id'] = $user_id;
        if(!empty($agent_id)) $where['agent_id'] = $agent_id;
        if(!empty($level_id)) $where['level_id'] = $level_id;
        if(!empty($province_id)) $where['province_id'] = $province_id;
        if(!empty($city_id)) $where['city_id'] = $city_id;
        if(!empty($nick)) $where['nick'] = array("like","%{$nick}%");
        if(!empty($phone)) $where['phone'] = array("like","%{$phone}%");
        if(!empty($identity)) $where['identity'] = $identity;
        if(!empty($real)) $where['real'] = array("like","%{$real}%");
        if(!empty($is_social)) $where['is_social'] = $is_social;
        if(!empty($is_accumulation_fund)) $where['is_accumulation_fund'] = $is_accumulation_fund;
        if(!empty($is_marry)) $where['is_marry'] = $is_marry;
        if(!empty($is_white)) $where['is_white'] = $is_white;
        if(!empty($is_loan)) $where['is_loan'] = $is_loan;
        if(!empty($is_overdue)) $where['is_overdue'] = $is_overdue;
        if(!empty($is_student)) $where['is_student'] = $is_student;
        if(!empty($is_real)) $where['is_real'] = $is_real;
        if(!empty($is_car)) $where['is_car'] = $is_car;
        if(!empty($is_house)) $where['is_house'] = $is_house;
        if(!empty($is_credit_card)) $where['is_credit_card'] = $is_credit_card;
        if(!empty($is_policy)) $where['is_policy'] = $is_policy;
        if(!empty($is_company)) $where['is_company'] = $is_company;
        if(!empty($source)) $where['source'] = $source;
        $user_list = IDb::getInstance('user')->setDbWhere($where)->setDbOrder("user_id desc")->pag();

        //取得用户等级信息
//        $user_level_list = IUserLevelCache::getList();

        //取得取得区域信息
        $area_list = IAreaCache::getAllList(2);

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("agent_id",$agent_id);
        $this->assign("level_id",$level_id);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("identity",$identity);
        $this->assign("real",$real);
        $this->assign("area",$area);
        $this->assign("is_social",$is_social);
        $this->assign("is_accumulation_fund",$is_accumulation_fund);
        $this->assign("is_marry",$is_marry);
        $this->assign("is_white",$is_white);
        $this->assign("is_loan",$is_loan);
        $this->assign("is_overdue",$is_overdue);
        $this->assign("is_student",$is_student);
        $this->assign("is_real",$is_real);
        $this->assign("is_car",$is_car);
        $this->assign("is_house",$is_house);
        $this->assign("is_credit_card",$is_credit_card);
        $this->assign("is_policy",$is_policy);
        $this->assign("is_company",$is_company);
        $this->assign("source",$source);
        $this->assign("area_list",$area_list);
        $this->assign("user_list",$user_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：用户基础信息
     * @开发者：cxl
     * @return string
     */
    public function user_info(){
        //接收参数
        $user_id = IRequest::get("user_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //设置查询条件
        $user_info = IUserCache::getInfo($user_id);

        //处理逾期信息
        if(empty($user_info['overdue_info'])){
            $user_info['overdue_info'] = [];
        }else{
            $user_info['overdue_info'] = json_decode($user_info['overdue_info'],true);
        }

        //取得代理商数据
        $user_company_where['user_id'] = $user_id;
        $user_company_list = IDb::getInstance('asset_company')->setDbWhere($user_company_where)->setDbOrder("is_history desc")->sel();

        //取得代理商数据
        $user_car_where['user_id'] = $user_id;
        $user_car_where['is_history'] = IsHistory::not;
        $user_car_list = IDb::getInstance('asset_car')->setDbWhere($user_car_where)->setDbOrder("user_car_id")->sel();

        //取得代理商数据
        $user_house_where['user_id'] = $user_id;
        $user_house_where['is_history'] = IsHistory::not;
        $user_house_list = IDb::getInstance('asset_house')->setDbWhere($user_house_where)->setDbOrder("user_house_id")->sel();

        //取得代理商数据
        $user_credit_card_where['user_id'] = $user_id;
        $user_credit_card_where['is_history'] = IsHistory::not;
        $user_credit_card_list = IDb::getInstance('asset_credit_card')->setDbWhere($user_credit_card_where)->setDbOrder("user_credit_card_id")->sel();

        //取得代理商数据
        $user_policy_where['user_id'] = $user_id;
        $user_policy_where['is_history'] = IsHistory::not;
        $user_policy_list = IDb::getInstance('asset_policy')->setDbWhere($user_policy_where)->setDbOrder("user_policy_id")->sel();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_info",$user_info);
        $this->assign("overdue_info",$user_info['overdue_info']);
        $this->assign("user_company_list",$user_company_list);
        $this->assign("user_car_list",$user_car_list);
        $this->assign("user_house_list",$user_house_list);
        $this->assign("user_credit_card_list",$user_credit_card_list);
        $this->assign("user_policy_list",$user_policy_list);

        //渲染模板输出
        return view("user_info_1");
    }

    /**
     * @功能：禁用用户
     * @开发者：cxl
     * @return string
     */
    public function user_status_del(){
        //接收参数
        $user_id = IRequest::get("user_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //设置查询条件
        $where['user_id'] = $user_id;
        $data['user_status'] = UserStatus::blacklist;
        $user_upd = IDb::getInstance('user')->setDbWhere($where)->setDbData($data)->upd();
        if($user_upd === false){
            error("禁用用户失败！");
        }

        //清除用户存在
        IUserCache::deleteInfo($user_id);

        //画面跳转
        $this->redirect("user_list");
    }

    /**
     * @功能：启用用户
     * @开发者：cxl
     * @return string
     */
    public function user_status_reset(){
        //接收参数
        $user_id = IRequest::get("user_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //设置查询条件
        $where['user_id'] = $user_id;
        $data['user_status'] = UserStatus::normal;
        $user_upd = IDb::getInstance('user')->setDbWhere($where)->setDbData($data)->upd();
        if($user_upd === false){
            error("启用用户失败！");
        }

        //清除用户存在
        IUserCache::deleteInfo($user_id);

        //画面跳转
        $this->redirect("user_list");
    }

    /**
     * @功能：用户申请材料信息历史
     * @开发者：cxl
     * @return string
     */
    public function user_material_history(){
        //接收参数
        $user_id = IRequest::get('user_id',IRequest::NOT_EMPTY,"用户编号不能为空！");
        $material_item_id = IRequest::get('material_item_id',IRequest::NOT_EMPTY,"用户材料项编号不能为空！");

        //设置查询条件
        $user_material_where['user_id'] = $user_id;
        $user_material_where['material_item_id'] = $material_item_id;
        $user_material_list = IDb::getInstance('user_material')->setDbWhere($user_material_where)->setDbOrder("user_id,is_history,material_id desc")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_material_list",$user_material_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：用户房产记录
     * @开发者：cxl
     * @return string
     */
    public function user_house_list(){
        //接收参数
        $user_id = IRequest::get('user_id',IRequest::NOT_EMPTY,"客户编号不能为空！");

        //设置查询条件
        $user_house_where['user_id'] = $user_id;
        $user_house_list = IDb::getInstance('asset_house')->setDbWhere($user_house_where)->setDbOrder("user_id,user_house_id")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("user_house_list",$user_house_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：用户房产详情
     * @开发者：cxl
     * @return string
     */
    public function user_house_info(){
        //接收参数
        $user_house_id = IRequest::get('user_house_id',IRequest::NOT_EMPTY,"用户房产编号不能为空！");

        //设置查询条件
        $user_house_where['user_house_id'] = $user_house_id;
        $user_house_info = IDb::getInstance('asset_house')->setDbWhere($user_house_where)->row();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_house_info",$user_house_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：用户保单记录
     * @开发者：cxl
     * @return string
     */
    public function user_policy_list(){
        //接收参数
        $user_id = IRequest::get('user_id',IRequest::NOT_EMPTY,"客户编号不能为空！");

        //设置查询条件
        $user_policy_where['user_id'] = $user_id;
        $user_policy_list = IDb::getInstance('asset_policy')->setDbWhere($user_policy_where)->setDbOrder("user_id,user_policy_id")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("user_policy_list",$user_policy_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：用户保单详情
     * @开发者：cxl
     * @return string
     */
    public function user_policy_info(){
        //接收参数
        $user_policy_id = IRequest::get('user_policy_id',IRequest::NOT_EMPTY,"用户保单编号不能为空！");

        //设置查询条件
        $user_policy_where['user_policy_id'] = $user_policy_id;
        $user_policy_info = IDb::getInstance('asset_policy')->setDbWhere($user_policy_where)->row();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_policy_info",$user_policy_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：用户车辆记录
     * @开发者：cxl
     * @return string
     */
    public function user_car_list(){
        //接收参数
        $user_id = IRequest::get('user_id',IRequest::NOT_EMPTY,"客户编号不能为空！");

        //设置查询条件
        $user_car_where['user_id'] = $user_id;
        $user_car_list = IDb::getInstance('asset_car')->setDbWhere($user_car_where)->setDbOrder("user_id,user_car_id")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("user_car_list",$user_car_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：用户车辆详情
     * @开发者：cxl
     * @return string
     */
    public function user_car_info(){
        //接收参数
        $user_car_id = IRequest::get('user_car_id',IRequest::NOT_EMPTY,"用户车辆编号不能为空！");

        //设置查询条件
        $where['user_car_id'] = $user_car_id;
        $user_car_info = IDb::getInstance('asset_car')->setDbWhere($where)->row();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_car_info",$user_car_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：用户信用卡记录
     * @开发者：cxl
     * @return string
     */
    public function user_credit_card_list(){
        //接收参数
        $user_id = IRequest::get('user_id',IRequest::NOT_EMPTY,"客户编号不能为空！");

        //设置查询条件
        $user_credit_card_where['user_id'] = $user_id;
        $user_credit_card_list = IDb::getInstance('asset_credit_card')->setDbWhere($user_credit_card_where)->setDbOrder("user_id,user_credit_card_id")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("user_credit_card_list",$user_credit_card_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：用户信用卡详情
     * @开发者：cxl
     * @return string
     */
    public function user_credit_card_info(){
        //接收参数
        $user_credit_card_id = IRequest::get('user_credit_card_id',IRequest::NOT_EMPTY,"用户车辆编号不能为空！");

        //设置查询条件
        $where['user_credit_card_id'] = $user_credit_card_id;
        $user_credit_card_info = IDb::getInstance('asset_credit_card')->setDbWhere($where)->row();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_credit_card_info",$user_credit_card_info);

        //渲染模板输出
        return view();
    }
}
